package news.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;


import shanshuitiancheng.news.MainActivity;
import shanshuitiancheng.news.R;


/**
 * Created by cch on 2016/6/8.
 */
public class MenuFragment extends Fragment implements AdapterView.OnItemClickListener {
    private View view;
    private String tag = "MenuFragment";

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(tag, "onActivityCreated");
        ListView list_view = (ListView) view.findViewById(R.id.list_view);
        initData();
           /* arrayadapter 的参数讲解
           创建ArrayAdapter时指定的三个参数说明如下：
        Contex：:整个应用的上下文。
        textViewResourceId：资源ID，代表一个TextView，用作ArrayAdapter的列表组件。
        objects：列表项中的数据*/
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,android.R.id.text1, initData());
//        将数据绑定到适配器中
        list_view.setAdapter(adapter);
//        设置一个点击事件
        list_view.setOnItemClickListener(this);
    }
        private List<String> initData(){
            List<String> list = new ArrayList<String>();
            list.add("fragment1");
            list.add("fragment2");
            list.add("fragment3");
            list.add("fragment4");
            list.add("fragment5");
            return list;
        }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(tag, "onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.i(tag, "onCreateView");
         view = LayoutInflater.from(getActivity()).inflate(R.layout.list_view, null);
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Fragment f = null;
        switch (position) {
            case 0:
                f = new Fragment1();
                Log.i("被点击了",position+"我是界面一");
                break;
            case 1:
                f = new Fragment2();
                Log.i("被点击了",position+"我是界面二");
                break;
            case 2:
                f = new Fragment3();
                Log.i("被点击了",position+"我是界面三");
                break;
            case 3:
                f = new Fragment4();
                Log.i("被点击了",position+"我是界面四");
                break;
            case 4:
                f = new Fragment5();
                Log.i("被点击了",position+"我是界面五");
                break;
        }
        switchFragment(f);
    }

    private void switchFragment(Fragment f) {
        // 设置fragment初始化页面
        if(f != null){
            if(getActivity()instanceof MainActivity){
                ((MainActivity) getActivity()).switchFragment(f);
            }
        }
    }
}
